<template>
	<view class="content">
		<u-cell-group :border="false">
			<!-- #ifndef MP -->
			<u-cell title="版本更新" size="large" :titleStyle="titleStyle" @click="getAppInfo">
				<view slot="right-icon" class="button flex">
					<text class="text">更新版本</text>
				</view>
				<text class="value" slot="value">v{{ version }}</text>
			</u-cell>
			<!-- #endif -->
			<u-cell size="large" title="清除内存/缓存" :titleStyle="titleStyle" @click="clearCache">
				<view slot="right-icon" class="button flex">
					<text class="text">清除缓存</text>
				</view>
				<text class="value" slot="value">{{ cacheSize }}</text>
			</u-cell>
			
			<u-cell :border="true" @click="openPage(2)" title="用户协议" size="large" :titleStyle="titleStyle" is-link></u-cell>
			<u-cell :border="true" @click="openPage(1)" title="隐私政策" size="large" :titleStyle="titleStyle" is-link></u-cell>
			
			<u-cell :border="false" title="注销账号" size="large" :titleStyle="titleStyle" @click="showLogoutModal = true">
				<view slot="right-icon" class="button flex" style="background: #f56c6c;">
					<text class="text">注销</text>
				</view>
			</u-cell>
		</u-cell-group>

		<u-modal :show="showLogoutModal" title="注销账号协议" :content="logoutAgreement" show-cancel-button confirm-text="同意并注销"
			cancel-text="取消" @confirm="handleLogout" @cancel="showLogoutModal = false" />
	</view>
</template>

<script>
	import {
		checkUpdate
	} from "@/components/yzhua006-update/js/app-update-check.js";
	import { toRichWeb } from '@/utils/system.js'
	export default {
		data() {
			return {
				titleStyle: {
					fontSize: '28rpx',
					color: '#777',
					fontWeight: '500',
					fontFamily: 'PINGFANG'
				},
				version: '',
				cacheSize: 0,
				showLogoutModal: false,
				logoutAgreement: `1. 注销账号后，您的所有数据将被清空且无法恢复。
2. 注销后将无法使用本账号登录本应用。
3. 如需再次使用，请重新注册账号。
请确认您已了解并同意以上内容。`
			}
		},
		onLoad() {
			this.getCacheSize();
			this.getVersion();
		},

		methods: {
			
			// 打开链接
			openPage(type) {
				toRichWeb(type)
			},

			// 获取版本号
			getVersion() {
				const {
					appVersion,
					appVersionCode
				} = uni.getSystemInfoSync();
				this.version = appVersion;
			},

			// 获取缓存大小
			getCacheSize() {
				const res = uni.getStorageInfoSync();
				this.cacheSize = this.convertSize(res.currentSize)
			},

			clearCache() {
				uni.showLoading({
					title: '正在清理中...',
					mask: true
				})
				setTimeout(() => {
					uni.hideLoading();
					this.cacheSize = 0;
				}, 1000)
			},

			convertSize(kb) {
				if (kb >= 1024 * 1024) {
					// 大于等于 1 GB
					const gb = kb / (1024 * 1024);
					return `${gb.toFixed(2)} GB`;
				} else if (kb >= 1024) {
					// 大于等于 1 MB
					const mb = kb / 1024;
					return `${mb.toFixed(2)} MB`;
				} else {
					// 小于 1 MB
					return `${kb} KB`;
				}
			},

			// 获取线上APP版本信息
			getAppInfo(type = 0) {
				const {
					appVersion,
					appVersionCode,
					platform
				} = uni.getSystemInfoSync();
				let params = {
					platform: platform,
					currentVersion: appVersionCode
				}
				if (params.platform != 'ios' && params.platform != 'android') false; //如果不是安卓或ios 返回false

				this.$http_api.checkAppVersion(params).then(res => {
					if (!res) {
						return uni.$u.toast('当前已是最新版！');
					}
					const {
						versionCode,
						versionNum,
						downloadUrl,
						updateDesc,
						isForce
					} = res;
					let update_info = {
						version: versionCode, //线上版本
						now_url: downloadUrl, //更新链接
						silent: 0, //是否是静默更新
						force: isForce, //是否是强制更新
						net_check: 1, //非WIfi是否提示
						note: updateDesc, //更新内容
					}
					checkUpdate(update_info, type).then(res => {
						if (res.msg) {
							plus.nativeUI.toast(res.msg);
						}
					});
				})
			},

			handleLogout() {
				this.showLogoutModal = false;
				uni.showLoading({
					title: '正在注销...',
					mask: true
				});
				this.$http_api.accountCancel({}).then(res => {
					console.log(res)
				  if (res == true) {            // 业务成功
				    uni.hideLoading();
					uni.clearStorageSync()
				    uni.$u.toast('注销成功');
					setTimeout(()=>{
						uni.reLaunch({ url: '/pages/public/login' })
					}, 1500);
				  } else {
				    uni.$u.toast(res.msg)
				  }
				}).catch(res => {
				  
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background: #fff;
		margin: 20rpx;
		border-radius: 28rpx;
		overflow: hidden;
	}

	.button {
		background: #1C6EFB;
		border-radius: 28rpx;
		width: 122rpx;
		height: 44rpx;
		justify-content: center;
		margin-left: 20rpx;

		.text {
			font-size: 24rpx;
			font-weight: 400;
			font-family: "PINGFANG";
			color: #fff;
		}
	}

	.value {
		font-size: 24rpx;
		color: #AAA;
		font-family: "PINGFANG";
	}
</style>